# Contributor (MSYS2): David Macek <david.macek.0@gmail.com>
# Maintainer (Arch Linux): Anatol Pomozov <anatol pomozov at gmail>
# Contributor (Arch Linux): Daniel YC Lin <dlin.tw at gmail>
# Contributor (Arch Linux): Konstantin Plotnikov <kostyapl at gmail dot com>

_realname=fossil
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.22
_pkgsha=66ee0beb
pkgrel=1
pkgdesc='Simple, high-reliability, distributed software configuration management (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
license=('BSD')
url='https://www.fossil-scm.org/'
depends=("${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-tcl")
optdepends=("${MINGW_PACKAGE_PREFIX}-putty: SSH transport")
source=("https://www.fossil-scm.org/home/tarball/${_pkgsha}/${_realname}-src-${pkgver}.tar.gz"
        "win.patch"
        "readline.patch"
        "win-tcl-unix-path.patch")
sha256sums=('b90c40beb75308b0eba46b5add6e54a7a9e65ac90ce86ec8d78ae2a4bb8bae60'
            '93e1bc5ff79e0df414fa0858894477bc41bc4a33fd6ec15ec3b7d5771e1d7090'
            '887c8b16de984156cb13bfdbc5715b1eff0123af0d07e6dc08265dbef0a1d519'
            '96090936b1aee3539211c400a4c8a7a9475c71f47238967a1924229798a2910b')

prepare() {
  cd "${srcdir}/${_realname}-src-${pkgver}"
  patch -p1 -i "${srcdir}/win.patch"
  patch -p0 -i "${srcdir}/readline.patch"
  patch -p1 -i "${srcdir}/win-tcl-unix-path.patch"
  # this doesn't get expanded, so delete it just to be sure
  sed -i '/ENABLE_MINIZ/d' Makefile.in
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL=--prefix \
  ../${_realname}-src-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-zlib=${MINGW_PREFIX} \
    --debug \
    --disable-internal-sqlite

  make \
    CFLAGS+="-DBROKEN_MINGW_CMDLINE=not_actually_broken"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-src-${pkgver}/COPYRIGHT-BSD2.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
